package com.anhtuan.reminder.providers;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class TasksContentProvider extends ContentProvider{	
	
	public static final String AUTHORITY = "com.anhtuan.reminder.providers.taskscontentprovider";
	
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "task.db";

	public static final String TASKS_TABLE = "task";
	public static final String TASKS_ID = TaskColumns._ID;
	
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + TASKS_TABLE);
	
	private static final String TASKS_TABLE_CREATE = "CREATE TABLE " + TASKS_TABLE + " (" +
			TaskColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
			TaskColumns.ACTIVE + " BOOL, " +
			TaskColumns.ACTION + " INTEGER, " +
			TaskColumns.REPEAT + " INTEGER, " +
			TaskColumns.DAY + " INTEGER, " + 
			TaskColumns.FIRST_CONTENT + " TEXT, " +
			TaskColumns.SECOND_CONTENT + " TEXT, " + 
			TaskColumns.THIRD_CONTENT + " TEXT);";
	
	private SQLiteDatabase database;
	private DataBaseHelper dbHelper;
	
	
	private Context context;
	
	
	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		int count = 0;
		
		count = database.delete(TASKS_TABLE, whereClause, whereArgs);
		
		if (count > 0) {
			context.getContentResolver().notifyChange(uri, null);
		}
		return count;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowId = database.insert(TASKS_TABLE, null, values);
		if (rowId > 0) {
			Uri rowUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
			context.getContentResolver().notifyChange(rowUri, null);
			return rowUri;
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		this.context = getContext();
		dbHelper = new DataBaseHelper(context);
		database = dbHelper.getWritableDatabase();
		
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(TASKS_TABLE);
		
		Cursor c = qb.query(database, projection, selection, selectionArgs, null, null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);
		
		return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int count = 0;
		count = database.update(TASKS_TABLE, values, selection, selectionArgs);
		
		if (count > 0) {
			context.getContentResolver().notifyChange(uri, null);
		}
		
		return count;
	}
	
	private static class DataBaseHelper extends SQLiteOpenHelper {

		public DataBaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(TASKS_TABLE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
		
	}
}
